//大部分构建工具都是基于nodejs平台运行的,且默认采用commonjs模式
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const projectRoot = path.resolve(__dirname, '../')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
function resolve(dir) {
  return path.join(__dirname, '..', dir)
}


module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
     filename: "[name].[hash:6].js",
    path: config.build.assetsRoot,
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
          test: /\.js$/,
          //排除掉node_modules文件
          exclude: /(node_modules)/,
          include: projectRoot,
        loader: "babel-loader",
          options: {
              plugins: [],
          },
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 1024 * 1,
          name: utils.assetsPath('[name].[hash:6].[ext]'),
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 1024 * 1,
          name: utils.assetsPath('[name].[hash:6].[ext]'),
          esModule: false,
        }
      },
      {
        test: /\.html$/,
        loader: "html-loader",
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(ts|tsx)$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          configFile: path.resolve(__dirname, '../tsconfig.json')
        }
      },
    ]
  },
  plugins:[
    new VueLoaderPlugin()
  ],
  externals: {
   'Vue':'vue',
   'Vuex':'vuex',
    'Router': 'vue-router'
  },
  resolve: {
      extensions: ['.js', '.vue', '.json','.ts'],
      alias: {
          'vue$': 'vue/src/vue.esm.js',
          "@": resolve("src"),
          "@c": resolve("src/component"),
          "@a": resolve("src/assets"),
          "@m": resolve("src/module"),
          '@u': resolve("src/utils"),
          '@config': resolve("src/config"),
          "@constant": resolve("src/constant.js"),
          "@utils": resolve("src/utils/index.js"),
          "@api": resolve("src/api")
      }
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}


